package arpui.bean;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import arpui.util.ARPFindUtil;

public class BaseArp {
	public static final short DISC_OPER = ARPFindUtil.bytesToShort(new byte[]{(byte) 0x00,0x01});
	public static final short DISC_ACK_OPER = ARPFindUtil.bytesToShort(new byte[]{(byte) 0x00,0x02});
	
	public static final short LOGIN_OPER = ARPFindUtil.bytesToShort(new byte[]{(byte) 0x00,0x07});
	public static final short LOGIN_ACK_OPER = ARPFindUtil.bytesToShort(new byte[]{(byte) 0x00,0x08});
	
	public static final short CONFIG_OPER = ARPFindUtil.bytesToShort(new byte[]{(byte) 0x02,0x01});
	public static final short CONFIG_ACK_OPER = ARPFindUtil.bytesToShort(new byte[]{(byte) 0x02,0x02});
	
	public long dstMac;					//6位
	public long srcMac;					//6位 
 
	public char type = ARPFindUtil.bytesToChar(new byte[]{0x08,0x06});							//0x08,0x06
	public char hardwareType = ARPFindUtil.bytesToChar(new byte[]{0x00,0x01});			//0x00,0x01
	public short protocalType= ARPFindUtil.bytesToShort(new byte[]{(byte) 0xf0,0x00});				// 0xf0,0x00
	public char version = ARPFindUtil.bytesToChar(new byte[]{0x01,0x00});							//0x01,0x00
	public short length;							//version及version以后    2位
	public int connID;							//0x00,0x00,0x00,0x00
	public short operator;					//操作码   2wei

	public byte[] commandData = null;					//数据
	//所有数据
	public byte[] allDatas = null;
	
	public BaseArp(){
		
	}
	
	public BaseArp(byte[] datas){
		try {
			if(datas.length<28){
				return;
			}
			this.allDatas = datas;
			
			ByteArrayInputStream bais = new ByteArrayInputStream(datas);
			DataInputStream dis = new DataInputStream(bais);
			
			byte[] tmpDMac = new byte[6];
			byte[] tmpSMac = new byte[6];
			dis.readFully(tmpDMac);
			dis.readFully(tmpSMac);
			dstMac = ARPFindUtil.macBytesToLong(tmpDMac);
			srcMac = ARPFindUtil.macBytesToLong(tmpSMac);
			
			type = dis.readChar();
			hardwareType = dis.readChar();
			protocalType = dis.readShort();
			version = dis.readChar();
			length = dis.readShort();
			connID = dis.readInt();
			operator = dis.readShort();
			
			commandData = new byte[datas.length -28];
			dis.readFully(commandData);
			
			bais.close();
			dis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public BaseArp(long dmac,long smac){
		dstMac = dmac;
		srcMac = smac;
	}
	
	public byte[] toBytesArray(){
		byte[] datas = null;
		//计算长度
		length= (short) (10+commandData.length);
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(baos);
			
			dos.write(ARPFindUtil.longToBytes(dstMac));
			dos.write(ARPFindUtil.longToBytes(srcMac));
			
			dos.writeChar(type);
			dos.writeChar(hardwareType);
			dos.writeShort(protocalType);
			dos.writeChar(version);
			dos.writeShort(length);
			dos.writeInt(connID);
			dos.writeShort(operator);
			
			dos.write(commandData);
			
			datas = baos.toByteArray();
			baos.close();
			dos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return datas;
	}
	
}
